package com.dingwei.superiordiary.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.ViewPagerAdatper;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DepthPageTransformer;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/3/28.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class GuideActivity extends BaseActivity {
    @Bind(R.id.in_viewpager)
    ViewPager inViewpager;
    @Bind(R.id.btn_next)
    Button btNext;
    @Bind(R.id.activity_main)
    RelativeLayout activityMain;
    private List<View> mViewList;
    private EdgeEffectCompat leftEdge;
    private EdgeEffectCompat rightEdge;

    @Override
    protected int getContentViewId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window window = getWindow();
        window.setFlags(flag, flag);
        return R.layout.activity_guide;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        if (!BaseActivity.value.getIsFirst()){
            intent.setClass(context,WelcomeActivity.class);
            startActivity(intent);
            finish();
        }else {
            mViewList = new ArrayList<View>();
            LayoutInflater lf = getLayoutInflater().from(this);
            View view1 = lf.inflate(R.layout.we_indicator1, null);
            View view2 = lf.inflate(R.layout.we_indicator2, null);
            View view3 = lf.inflate(R.layout.we_indicator3, null);
            mViewList.add(view1);
            mViewList.add(view2);
            mViewList.add(view3);
            inViewpager.setAdapter(new ViewPagerAdatper(mViewList));
            inViewpager.setPageTransformer(true,new DepthPageTransformer());
            value.setIsFirst(false);

            try {
                Field leftEdgeField = inViewpager.getClass().getDeclaredField("mLeftEdge");
                Field rightEdgeField = inViewpager.getClass().getDeclaredField("mRightEdge");
                if (leftEdgeField !=null  && rightEdgeField !=null ) {
                    leftEdgeField.setAccessible(true);
                    rightEdgeField.setAccessible(true);
                    leftEdge = (EdgeEffectCompat) leftEdgeField.get(inViewpager);
                    rightEdge = (EdgeEffectCompat) rightEdgeField.get(inViewpager);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            inViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    if(rightEdge!=null&&!rightEdge.isFinished()){//到了最后一张并且还继续拖动，出现蓝色限制边条了
                        intent.setClass(context,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                @Override
                public void onPageSelected(int arg0) {
                }
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                }
            });
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {
        intent.setClass(context,MainActivity.class);
        startActivity(intent);
        finish();
    }
}
